package com.rmd.dl.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.ocpsoft.prettytime.PrettyTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.rmd.dl.domain.po.DlTopic;
import com.rmd.dl.domain.po.DlTopicExample;
import com.rmd.dl.domain.vo.TopicVO;
import com.rmd.dl.mapper.DlTopicMapper;
import com.rmd.dl.service.TopicService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Service
public class TopicServiceImpl implements TopicService{

	@Autowired
	private DlTopicMapper dlTopicMapper;

	public JSONObject queryObject(Integer id) {
		JSONObject jsonObject = new JSONObject();
		DlTopicExample example = new DlTopicExample();
		DlTopic topic = dlTopicMapper.selectByPrimaryKey(id);
		jsonObject.put("data", topic);
		jsonObject.put("message", "查询成功");
		jsonObject.put("code", 0);
		return jsonObject;
	}

	public JSONObject queryList(DlTopic dlTopic) {
		JSONObject jsonObject = new JSONObject();
		DlTopicExample example = new DlTopicExample();
		List<DlTopic> list = dlTopicMapper.selectByExample(example);
		JSONArray array = new JSONArray();
		/*for(DlTopic topic:list){
			array.add(topic);
		}*/
		TopicVO topicVo = null;
		for(DlTopic topic:list){
			topicVo = new TopicVO();
			BeanUtils.copyProperties(topic, topicVo);
			topicVo.setInTimeDesc(formatDate(topicVo.getInTime()));
			array.add(topicVo);
		}
		jsonObject.put("data", array);
		jsonObject.put("message", "查询成功");
		jsonObject.put("code", 0);
		return jsonObject;
	}

	public JSONObject queryTotal(DlTopic dlTopic) {
		// TODO Auto-generated method stub
		return null;
	}

	public JSONObject save(DlTopic dlTopic) {
		// TODO Auto-generated method stub
		return null;
	}

	public JSONObject update(DlTopic dlTopic) {
		// TODO Auto-generated method stub
		return null;
	}

	public JSONObject deleteBatch(Long[] ids) {
		// TODO Auto-generated method stub
		return null;
	}
	
	public String formatDate(Date date) {
        String dateStr = "";
        if (date != null) {
            PrettyTime prettyTime = new PrettyTime(Locale.CHINESE);
            dateStr = prettyTime.format(date);
        }
        return dateStr;
    }
}
